﻿using System;

namespace IupNET
{
    public class ServerImage
    {
        internal IntPtr cobj = IntPtr.Zero;
        private bool disposed = false;

        public readonly int Width;
        public readonly int Height;
        
        public ServerImage(Canvas c, int w, int h)
        {
            cobj=Cdc.CanvasCreateImage(c.cobj, w, h);
            Width = w;
            Height = h;

            if (cobj == IntPtr.Zero)
                throw new Exception("Failed to create server image with width=" + w.ToString() + " height=" + h.ToString());
        }


        public ServerImage(Canvas c, int x, int y, int w, int h)
        {
            //copy a part of the specified canvas
            cobj = Cdc.CanvasCreateImage(c.cobj, w, h);
            Width = w;
            Height = h;

            if (cobj == IntPtr.Zero)
                throw new Exception("Failed to create server image with width=" + w.ToString() + " height=" + h.ToString());

            Copy(c,x, y);
        }

        public void Copy(Canvas c, int x, int y)
        {
            Cdc.CanvasGetImage(c.cobj, cobj, x, y);
        }

        ~ServerImage()
        {
            Dispose(false);
        }
       

        public void Dispose()
        {

            GC.SuppressFinalize(this);
            Dispose(true);
        }



        public void Dispose(bool disposing)
        {
            
            if (!disposed)
            {
                if (cobj != IntPtr.Zero)
                {
                    Cdc.KillImage(cobj);
                    cobj = IntPtr.Zero;
                }
            }
            disposed = true;
        
        }

    }
}
